#include "scripthighlighter.h"
#include <QRegularExpressionMatchIterator>

ScriptHighlighter::ScriptHighlighter(QTextDocument *parent)
  : QSyntaxHighlighter(parent)
{
  formats["filename"].setForeground(Qt::darkGray);
  formats["ffmpeg"].setForeground(Qt::darkGreen);
  formats["option"].setForeground(Qt::blue);
  formats["value"].setForeground(Qt::darkRed);

}

void ScriptHighlighter::highlightBlock(const QString &text)
{
  QRegularExpression e;
  QRegularExpressionMatchIterator i;

  e = QRegularExpression(" -[^ \"]+ ");
  i = e.globalMatch(text);
  while (i.hasNext()) {
    auto match = i.next();
    setFormat(match.capturedStart(), match.capturedLength(), formats["option"]);
  }

  e = QRegularExpression(" [^ \"-]+ ");
  i = e.globalMatch(text);
  while (i.hasNext()) {
    auto match = i.next();
    setFormat(match.capturedStart(), match.capturedLength(), formats["value"]);
  }

  e = QRegularExpression("\"[^\"]*\"");
  i = e.globalMatch(text);
  while (i.hasNext()) {
    auto match = i.next();
    setFormat(match.capturedStart(), match.capturedLength(), formats["filename"]);
  }

  e = QRegularExpression("\"[^\"]*ffmpeg\" ");
  i = e.globalMatch(text);
  while (i.hasNext()) {
    auto match = i.next();
    setFormat(match.capturedStart(), match.capturedLength(), formats["ffmpeg"]);
  }
}


